package com.um.jdy.platform.member.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.um.jdy.baofu.api.BaoFuAccountService;
import com.um.jdy.baofu.config.BFConfig;
import com.um.jdy.baofu.entity.account.AccInfoEntity;
import com.um.jdy.baofu.entity.account.BCT3100200101Entity;
import com.um.jdy.baofu.utils.FormatUtil;
import com.um.jdy.common.baofu.entity.po.BaofuUser;
import com.um.jdy.common.baofu.service.BaofuService;
import com.um.jdy.common.member.entity.vo.MemberUserVO;
import com.um.jdy.platform.member.entity.dto.MemberPageDTO;
import com.um.jdy.platform.member.manager.MemberUserManager;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.LoginRequired;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 会员管理
 *
 * @author cs
 * 日期 2023/5/25
 * 版权所有 广州优匠科技有限公司
 */
@Api(tags = "会员管理")
@Validated
@RestController
@RequestMapping("/api/v1/cms/member")
@ApiPermission("member")
@TokenRequired
@Slf4j
public class MemberUserController extends BaseController<MemberUserManager> {

//    @Autowired
//    private BFConfig zhongJinPayConfig;
    @Autowired
    private BaoFuAccountService baoFuAccountService;
    @Autowired
    private BaofuService baofuService;

    /**
     * 分页查询钱包
     *
     * @param pageDTO
     * @return
     */
    @ApiPermission("page")
    @ApiOperation("分页查询用户")
    @PostMapping("page")
    public Response<Page<MemberUserVO>> page(@RequestBody MemberPageDTO pageDTO) {
        PageParams pageParams = pageDTO.toPageParams(new String[]{"mobile","nickname"},new String[]{"mobile","nickname"});
        pageParams.getCondition().put("orderby", "create_time desc");
        return manager.page(pageParams);
    }

    @ApiPermission("get")
    @ApiOperation("详情")
    @GetMapping({"{id}"})
    public Response<MemberUserVO> get(@PathVariable String id) {
        return manager.get(id);
    }

    @ApiPermission("mobile_search")
    @ApiOperation("按手机号查询用户列表")
    @GetMapping({"list/{mobile}"})
    public Response<List<MemberUserVO>> getListByMobile(@PathVariable String mobile) {
        return manager.getListByMobile(mobile);
    }

    /**
     * 修改会员状态
     * @param id
     * @param status
     * @return
     */
    @ApiPermission("status")
    @ApiOperation("修改会员状态")
    @PutMapping("/{id}/status/{status}")
    public Response changeStatus(@PathVariable String id,@PathVariable String status){
        return manager.changeStatus(id,status);
    }


    /**
     * test

     * @return
     */
    @ApiOperation("修改会员状态")
    @PostMapping("/test")
    @LoginRequired(required = false)
    public Response test(){
        BCT3100200101Entity BodyInfo= new BCT3100200101Entity();
        BodyInfo.setAccType(2);//账户类型:1-个人,2-企业/个体

        //企业开户
        String Email = "greatwalltesta@baofu.com";//企业开户邮箱
        boolean SelfEmployed = false;//是否个体户 企业为false，不传默认为false
        String CorporateName ="杨雪宇";//法人姓名
        String CorporateCertType = "ID";//法人证件类型：身份证-ID，港澳通行证-HONG_KONG_AND_MACAO_PASS、台湾同胞来往内地通行证-TAIWAN_TRAVEL_PERMIT、护照-PASSPORT
        String CorporateCertId = "542624198102098035";//法人证件号码
        String CorporateMobile = "18756161713";//法人手机号
        String IndustryId="9999";
        String BankName="中国工商银行";//银行名称
        String DepositBankProvince ="江苏省";//开户行省份
        String DepositBankCity = "南京市";//开户行城市
        String DepositBankName = "支行营业部";//开户支行名称
        String RegisterCapital = "50000";//注册资本

        //公共
        String CardNo="7881620854713";//卡号 个人为对私卡号，企业为对公卡号
        String CustomerName ="琴海文科技股份有限公司";//客户名称,当为企业/个体时为企业名称
        String CertificateNo ="91510725913854383A";//证件号，当为个人时上送身份证号，企业时上送营业执照号

        AccInfoEntity accinfoEntity = new AccInfoEntity();
        accinfoEntity.setTransSerialNo("TSN"+FormatUtil.CreateAeskey(20));//每次不重复
        accinfoEntity.setLoginNo("LN"+ FormatUtil.CreateRandNum(15));//登录号商户端用户唯一标识，商户自定义（不重复）
        accinfoEntity.setCustomerName(CustomerName);//个人：客户名称与持卡人姓名一致 企业：商户名称（营业执照上的名称）
        accinfoEntity.setCertificateNo(CertificateNo);//个人：身份证号码，企业：营业执照证件号码
        accinfoEntity.setCardNo(CardNo);//个人：对私卡号，企业：对公卡号，（个体可绑法人对私卡号）
        accinfoEntity.setNeedUploadFile(false);//是否需要上传附件,true/false
        log.info("流水号："+accinfoEntity.getTransSerialNo());
        log.info("登录号："+accinfoEntity.getLoginNo());

        accinfoEntity.setCertificateType("LICENSE");
        accinfoEntity.setEmail(Email);
        accinfoEntity.setSelfEmployed(SelfEmployed);
        accinfoEntity.setCorporateName(CorporateName);
        accinfoEntity.setCorporateCertType(CorporateCertType);
        accinfoEntity.setCorporateCertId(CorporateCertId);
        if(SelfEmployed){
            accinfoEntity.setCorporateMobile(CorporateMobile);
        }
        accinfoEntity.setIndustryId(IndustryId);
        accinfoEntity.setBankName(BankName);
        accinfoEntity.setDepositBankProvince(DepositBankProvince);
        accinfoEntity.setDepositBankCity(DepositBankCity);
        accinfoEntity.setDepositBankName(DepositBankName);
        accinfoEntity.setRegisterCapital(RegisterCapital);

        if(accinfoEntity.isNeedUploadFile()){
            accinfoEntity.setQualificationTransSerialNo("");//[必传]资质文件流水,是否上传请咨询业务对接人
        }

        BodyInfo.setAccInfo(accinfoEntity);//开户具体信息根据类型不同,信息不同,现只支持单笔开户

        JSONObject json =  baoFuAccountService.BCT3_1002_001_01(BodyInfo);
        //添加数据库
        //BaofuUser baofuUser = new BaofuUser();
        BaofuUser baofuUser = BeanUtil.copyProperties(accinfoEntity, BaofuUser.class);
        //BeanUtils.copyProperties(accinfoEntity,baofuUser);
        baofuUser.setCreateTime(new Date());
        baofuUser.setIsDelete(0);
        baofuUser.setAccType(BodyInfo.getAccType()==1?1:accinfoEntity.isSelfEmployed()?2:3);
        baofuService.save(baofuUser);
        return Response.success(json);


    }
}
